package com.yanqu.road.server.manager.tomb.event;

import com.yanqu.road.logic.activity.tomb.config.entity.ActivityTombEvent;
import com.yanqu.road.logic.activity.tomb.config.entity.ActivityTombFloor;
import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.manager.tomb.Position;
import com.yanqu.road.server.manager.tomb.TombMaze;
import com.yanqu.road.server.manager.tomb.entity.DbTombLog;
import com.yanqu.road.server.manager.tomb.entity.DbTombPosition;
import com.yanqu.road.server.manager.tomb.entity.DbTombUser;
import com.yanqu.road.server.protocol.GameErrorCode;

public class Trap implements IEvent {
    @Override
    public boolean canStand(DbTombUser dbTombUser, TombMaze tombMaze, Position position) {
        return true;
    }

    @Override
    public boolean canThrough(DbTombUser dbTombUser, TombMaze tombMaze, Position position) {
        return true;
    }

    @Override
    public boolean canExplore(DbTombUser dbTombUser, TombMaze tombMaze, Position position) {
        return false;
    }

    @Override
    public void detail(TombMaze tombMaze, DbTombUser dbTombUser, Position position, Tomb.PositionTemp.Builder positionTempBuilder, Tomb.TombEventExploreResp.Builder builder) {
        DbTombPosition dbTombPosition = tombMaze.getDbTombPosition(position.getKey());
        long unionDigUser = dbTombPosition.acquireDigUser();
        if (unionDigUser == dbTombUser.getUserId()) {
            ActivityTombEvent activityTombEvent = tombMaze.getConfig().getEvent(dbTombPosition.getEventId());
            positionTempBuilder.setEnum(Tomb.PositionEnum.POSITION_EVENT_VALUE);
            positionTempBuilder.setEventConfig(activityTombEvent.getEventConfigBuilder());
            positionTempBuilder.setOverTime(dbTombPosition.getOverTime());
            builder.setIsSendHelp(dbTombPosition.getIsSendHelp() == 1);
            for (long aLong : dbTombPosition.getHelpUserIdList()) {
                DbTombUser dbTombUser1 = tombMaze.getActivity().getDbData().getDbTombUser(aLong);
                builder.addUnionHelpUserIdList(dbTombUser1.toPlayerBaseTempBuilder());
            }
            return;
        }
        positionTempBuilder.setIsOver(true);
    }

    @Override
    public void explore(TombMaze tombMaze, DbTombUser dbTombUser, DbTombPosition dbTombPosition, Position position, ActivityTombEvent activityTombEvent, Tomb.PositionTemp.Builder positionTempBuilder, Tomb.TombEventExploreResp.Builder builder) {
        dbTombUser.setStatus(1);

        long p1 = activityTombEvent.getParam1().get(0);
        dbTombPosition.setOverTime(System.currentTimeMillis() + (1000L * p1 * 60));
        positionTempBuilder.setOverTime(dbTombPosition.getOverTime());
        dbTombUser.setLockEvent(position.getKey());
        positionTempBuilder.setEnum(position.getpEnum());
        positionTempBuilder.setEventEnum(position.geteType());
        Tomb.CoordinateTemp.Builder coordinate = Tomb.CoordinateTemp.newBuilder();
        coordinate.setRow(position.getRow());
        coordinate.setColumn(position.getColumn());
        coordinate.setFloor(tombMaze.getFloor());
        builder.setStand(coordinate);

    }

    @Override
    public void display(DbTombUser dbTombUser, TombMaze tombMaze, Position position, Tomb.PositionTemp.Builder p) {
        DbTombPosition dbTombPosition = tombMaze.getDbTombPosition(position.getKey());
        if (dbTombPosition == null) {
            p.setEnum(Tomb.PositionEnum.POSITION_EVENT_DONE_VALUE);
            return;
        }
        long unionDigUser = dbTombPosition.acquireDigUser();
        if (unionDigUser != dbTombUser.getUserId()) {
            p.setEnum(Tomb.PositionEnum.POSITION_EVENT_DONE_VALUE);
            return;
        }
        p.setIsOver(false);
    }

    @Override
    public void info(TombMaze tombMaze, DbTombUser dbTombUser, DbTombPosition dbTombPosition, Position position, ActivityTombEvent activityTombEvent, Tomb.PositionTemp.Builder positionTempBuilder, Tomb.TombEventInfoResp.Builder builder) {
        if (!dbTombPosition.containUnionDig(dbTombUser.getUserInfo().getUnionUid())) {
            return;
        }
        long unionDigUser = dbTombPosition.acquireDigUser();
        if (unionDigUser != dbTombUser.getUserId()) {
            return;
        }
        positionTempBuilder.setEnum(Tomb.PositionEnum.POSITION_EVENT_VALUE);
        positionTempBuilder.setEventConfig(activityTombEvent.getEventConfigBuilder());
        positionTempBuilder.setOverTime(dbTombPosition.getOverTime());
        builder.setIsSendHelp(dbTombPosition.getIsSendHelp() == 1);
        for (long aLong : dbTombPosition.getHelpUserIdList()) {
            DbTombUser dbTombUser1 = tombMaze.getActivity().getDbData().getDbTombUser(aLong);
            builder.addUnionHelpUserIdList(dbTombUser1.toPlayerBaseTempBuilder());
        }
    }

    @Override
    public void touch(TombMaze tombMaze, DbTombUser dbTombUser, DbTombPosition dbTombPosition, Position position, ActivityTombEvent activityTombEvent, Tomb.PositionTemp.Builder positionTempBuilder, Tomb.TombEventTouchResp.Builder builder, Tomb.TombEventTouchReq req) {

        if (dbTombPosition.getDone() == 1) {
            builder.setRet(GameErrorCode.E_TOMB_EVENT_DONE);
            return;
        }

        long unionDigUser = dbTombPosition.acquireDigUser();
        if (unionDigUser != dbTombUser.getUserId()) {
            positionTempBuilder.setEnum(Tomb.PositionEnum.POSITION_EVENT_DONE_VALUE);
            return;
        }

        int p2 = activityTombEvent.getParam1().get(1);
        int p3 = activityTombEvent.getParam1().get(2);

        if (dbTombPosition.getOverTime() > System.currentTimeMillis() && dbTombPosition.getHelpUserIdList().size() < p3) {
            if (!tombMaze.costEnergy(dbTombUser, p2, activityTombEvent.getId())) {
                builder.setRet(GameErrorCode.E_TOMB_USER_NOT_ENOUGH_ENERGY);
                return;
            }
        } else {
            tombMaze.recoverEnergy(dbTombUser);

            ActivityTombFloor activityTombFloor = tombMaze.getConfig().getFloor(tombMaze.getFloor());
            int energy = activityTombFloor.getEnergy();

            if (dbTombPosition.getHelpUserIdList().size() >= p3) {
                //日志
                DbTombLog dbTombLog = DbTombLog.newInstance();
                dbTombLog.setUid(String.valueOf(dbTombUser.getUserId()));
                dbTombLog.setFloor(tombMaze.getFloor());
                dbTombLog.setFromUserId(dbTombUser.getUserId());
                dbTombLog.setLogEnum(Tomb.TombLogEnum.TombLogEnum_Energy_Trap_Escape_VALUE);
                dbTombLog.setEventId(activityTombEvent.getId());
                dbTombLog.setValue1(energy);
                dbTombLog.setValue(2);
                StringBuilder stringBuilder = new StringBuilder();
                for (long aLong : dbTombPosition.getHelpUserIdList()) {
                    DbTombUser dbTombUser1 = tombMaze.getActivity().getDbData().getDbTombUser(aLong);
                    stringBuilder.append(dbTombUser1.getUserInfo().getNickName());
                    stringBuilder.append("|");
                }
                if (dbTombPosition.getHelpUserIdList().size() > 0) {
                    stringBuilder.deleteCharAt(stringBuilder.length() - 1);
                }
                dbTombLog.setRewards(stringBuilder.toString());
                dbTombLog.setRewards(builder.getRewards());
                dbTombLog.setX(position.getRow());
                dbTombLog.setY(position.getColumn());
                dbTombLog.setDescription("被人救助了，体力返还");
                dbTombLog.setGroupId(dbTombUser.getGroupId());
                tombMaze.getActivity().getTombLog().addDbTombLog(dbTombLog, false);
            } else {
                //日志
                DbTombLog dbTombLog = DbTombLog.newInstance();
                dbTombLog.setUid(String.valueOf(dbTombUser.getUserId()));
                dbTombLog.setFloor(tombMaze.getFloor());
                dbTombLog.setFromUserId(dbTombUser.getUserId());
                dbTombLog.setLogEnum(Tomb.TombLogEnum.TombLogEnum_Energy_Trap_Escape_VALUE);
                dbTombLog.setEventId(activityTombEvent.getId());
                dbTombLog.setValue(1);
                dbTombLog.setValue1(energy);
                StringBuilder stringBuilder = new StringBuilder();
                for (long aLong : dbTombPosition.getHelpUserIdList()) {
                    DbTombUser dbTombUser1 = tombMaze.getActivity().getDbData().getDbTombUser(aLong);
                    stringBuilder.append(dbTombUser1.getUserInfo().getNickName());
                    stringBuilder.append("|");
                }
                if (dbTombPosition.getHelpUserIdList().size() > 0) {
                    stringBuilder.deleteCharAt(stringBuilder.length() - 1);
                }
                dbTombLog.setRewards(stringBuilder.toString());
                dbTombLog.setRewards(builder.getRewards());
                dbTombLog.setX(position.getRow());
                dbTombLog.setY(position.getColumn());
                dbTombLog.setGroupId(dbTombUser.getGroupId());
                dbTombLog.setDescription("时间到了,体力返还");
                tombMaze.getActivity().getTombLog().addDbTombLog(dbTombLog, false);
            }
        }

        dbTombUser.setStatus(0);

        //这里处理一下极端的情况，如果客户端，死亡的情况，掐脱了陷阱，把玩家的血量也恢复一下。
        if (!dbTombUser.isAlive()) {
            dbTombUser.setMaxHp(dbTombUser.getTotalHp());
            dbTombUser.setHp(dbTombUser.getTotalHp());
        }

        dbTombUser.refreshUpdateTime();

        if (dbTombPosition.getHelpUserIdList().size() >= p3) {

            //日志
            DbTombLog dbTombLog = DbTombLog.newInstance();
            dbTombLog.setUid(String.valueOf(dbTombUser.getUserId()));
            dbTombLog.setFloor(dbTombUser.getFloor());
            dbTombLog.setFromUserId(dbTombUser.getUserId());
            dbTombLog.setLogEnum(Tomb.TombLogEnum.TombLogEnum_Trap_Escape_VALUE);
            dbTombLog.setValue1(p2);
            if (dbTombPosition.getHelpUserIdList().size() < p3) {
                dbTombLog.setValue(p2);
            }
            StringBuilder stringBuilder = new StringBuilder();
            for (long aLong : dbTombPosition.getHelpUserIdList()) {
                DbTombUser dbTombUser1 = tombMaze.getActivity().getDbData().getDbTombUser(aLong);
                stringBuilder.append(dbTombUser1.getUserInfo().getNickName());
                stringBuilder.append("|");
            }
            if (dbTombPosition.getHelpUserIdList().size() > 0) {
                stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            }
            dbTombLog.setRewards(stringBuilder.toString());
            dbTombLog.setX(dbTombUser.getX());
            dbTombLog.setY(dbTombUser.getY());
            dbTombLog.setGroupId(dbTombUser.getGroupId());
            dbTombLog.setDescription("被人救助了，陷阱解除了");
            tombMaze.getActivity().getTombLog().addDbTombLog(dbTombLog, false);
        }


        //发到游戏服写日志
        Tomb.TombLogCrossToGameReq.Builder logBuilder = Tomb.TombLogCrossToGameReq.newBuilder();
        logBuilder.setLEnum(Tomb.GameTombLogEnum.TRAP);
        logBuilder.setUserId(dbTombUser.getUserId());
        logBuilder.setActivityId(tombMaze.getActivityId());
        logBuilder.setUnionUid(dbTombUser.getUserInfo().getUnionUid());
        logBuilder.setFloor(dbTombUser.getFloor());
        logBuilder.setX(position.getRow());
        logBuilder.setY(position.getColumn());
        logBuilder.setHp(dbTombUser.getHp());
        logBuilder.setTimestamp(System.currentTimeMillis());
        Tomb.TombLogTrap.Builder trap = Tomb.TombLogTrap.newBuilder();
        logBuilder.setTrap(trap);
        tombMaze.getActivity().sendLogToGame(dbTombUser, logBuilder);


        dbTombUser.setStatus(0);
        dbTombPosition.refreshUpdateTime();

        dbTombUser.setLockEvent("");
        dbTombUser.refreshUpdateTime();
        dbTombPosition.setDone(1);
        dbTombPosition.refreshUpdateTime();
        position.setpEnum(Tomb.PositionEnum.POSITION_EVENT_DONE_VALUE);
        tombMaze.eventDone(dbTombPosition, position);


        Tomb.AllyInfo.Builder allyInfoBuilder = dbTombUser.toAllyInfoBuilder();
        allyInfoBuilder.setOverTimestamp(dbTombPosition.getOverTime());
        tombMaze.getActivity().moveNotify(tombMaze.getFloor(), null, dbTombUser, null, allyInfoBuilder);
    }
}
